# Maintainer: Alexey Pavlov <Alexpux@gmail.com>

_realname=gtk-doc
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.32
pkgrel=1
pkgdesc="Documentation tool for public library API (mingw-w64)"
arch=('any')
url="https://www.gtk.org/gtk-doc/"
license=("GPL" "FDL")
options=(staticlibs strip)
depends=("${MINGW_PACKAGE_PREFIX}-docbook-xsl"
         "${MINGW_PACKAGE_PREFIX}-docbook-xml"
         "${MINGW_PACKAGE_PREFIX}-libxslt"
         "${MINGW_PACKAGE_PREFIX}-python3"
         "${MINGW_PACKAGE_PREFIX}-python3-pygments")
makedepends=("${MINGW_PACKAGE_PREFIX}-python3-setuptools"
             "${MINGW_PACKAGE_PREFIX}-itstool")
url="https://www.gtk.org/gtk-doc/"
source=(https://download.gnome.org/sources/${_realname}/${pkgver}/${_realname}-${pkgver}.tar.xz
        pyscript2exe.py)
sha256sums=('de0ef034fb17cb21ab0c635ec730d19746bce52984a6706e7bbec6fb5e0b907c'
            '00e03a8b9d45e620c6fabbf96f5ccb41d1e49cc9b35ccb846d423f235ad5bec6')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
}

build() {
  export MSYS2_ARG_CONV_EXCL="-//OASIS"

  mkdir -p "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"

  export PYTHON=${MINGW_PREFIX}/bin/python3
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --enable-shared \
    --enable-static \
    --with-xml-catalog=${MINGW_PREFIX}/etc/xml/catalog

  make

  for name in check fixxref mkdb mkhtml mkman mkpdf rebase scan scangobj ; do
    sed -s "s|'${MINGW_PREFIX}|sys.prefix + '|g" -i "gtkdoc-${name}"
  done

  echo -e "import sys\n$(cat gtkdoc/config.py)" > gtkdoc/config.py

  sed -s "s|'${MINGW_PREFIX}|sys.prefix + '|g" -i "gtkdoc/config.py"
}

package() {
  install=${_realname}-${CARCH}.install
  cd "${srcdir}/build-${MINGW_CHOST}"

  make DESTDIR="$pkgdir" install

  local _PRE_WIN="$(cygpath -m ${MINGW_PREFIX})"
  for name in check fixxref mkdb mkhtml mkman mkpdf rebase scan scangobj depscan mkhtml2; do
    ${MINGW_PREFIX}/bin/python3 \
      "${srcdir}/pyscript2exe.py" "${pkgdir}${MINGW_PREFIX}/bin/gtkdoc-${name}"
    sed -e "s|${_PRE_WIN}|${MINGW_PREFIX}|g" \
      -i "${pkgdir}${MINGW_PREFIX}/bin/gtkdoc-${name}-script.py"
  done
}
